package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.WaterDto;
import marchsoft.modules.selfFish.pisciculture.service.WaterService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**水质管理
 * @title FishController
 * @description 水质管理
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/water")
@Api(tags = "养鱼管理：水质管理")
public class WaterController {

    @Autowired
    private WaterService waterService;

    private static final String ENTITY_NAME = "water";

    @GetMapping
    @ApiOperation("获取水质列表")
    @PreAuthorize("@smpe.check('water:list')")
    public Result<Object> query(String waterTemperature,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获取水质列表 /api/water】操作人id：{}，水质查询条件 waterTemperature：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),waterTemperature, pageNum));
        return waterService.getWaterList(waterTemperature,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增水质")
    @PreAuthorize("@smpe.check('water:add')")
    public Result<Object> create(@RequestBody WaterDto waterDto){
        log.info(StrUtil.format("【新增水质 /api/water】操作人id：{}，新增水质实体：{}", SecurityUtils.getCurrentUserId(), waterDto));
        if (ObjectUtil.isNotNull(waterDto.getId())) {
            log.error(StrUtil.format("【新增水质失败】操作人id：{}，新增水质实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), waterDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return waterService.create(waterDto);
    }

    @DeleteMapping
    @ApiOperation("删除水质")
    @PreAuthorize("@smpe.check('water:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除水质 /api/water】操作人id：{}，被删除水质id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return waterService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改水质")
    @PreAuthorize("@smpe.check('water:edit')")
    public Result<Object> update(@RequestBody WaterDto waterDto){
        log.info(StrUtil.format("【修改水质 /api/water】操作人id：{}，被修改水质id：{}", SecurityUtils.getCurrentUserId(), waterDto.getId()));
        return waterService.update(waterDto);
    }

    @ApiOperation("导出水质数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('water:list')")
    public void download(HttpServletResponse response, String waterTemperature) throws IOException {
        log.info(StrUtil.format("【查询水质 /api/water】操作人id：{}，水质查询条件 waterTemperature：{}", SecurityUtils.getCurrentUserId(), waterTemperature));
        waterService.download(waterTemperature,response);
    }
}
